const { query } = require('express');
const express = require('express');
//引入mysql模块
const pool = require('../pool.js');
//创建路由
const router = express.Router();

//爆款套装列表
router.get('/list',(req,res)=>{
        
        pool.query(`select cp.*,sp.svalue,sp.socre,s.* from compose cp join s_c c on cp.zid=c.skuid join shopexpand sp on sp.skuid=c.skuids join shop s on s.id = sp.cid where cp.cateid=101`,(err,r)=>{
            if(err){
                return  res.send({code:400,msg:'出现了错误',err:err});
                
            }
            
            if(r.length == 0){
                return res.send({code:400,msg:'查询失败'});
            }else{
                return res.send({code:200,title:'稀缺显卡 超值板U 限量抢购',msg:'查询成功！',data:r});
            }
        });
        
});
//爆款套装详情
router.get('/details',(req,res)=>{
    
    pool.query('select sp.svalue,sp.socre,s.* from compose cp join s_c c on cp.zid=c.skuid join shopexpand sp on sp.skuid=c.skuids join shop s on s.id = sp.cid where cp.zid=?',[req.query.zid],(err,r)=>{
        if(err){
            res.send({code:400,msg:'出现了错误',err:err});


            return;
        };

        if(r.length == 0){
            res.send({code:400,msg:'查询失败'});
        }else{
            pool.query('select * from compose where zid=?',[req.query.zid],(err,db)=>{
                if(err){
                    return res.send({code:400,msg:'出现了错误',err:err});
                };
                if(db.length == 0){
                    return res.send({code:400,msg:'查询失败'});
                }else{
                    return res.send({code:200,msg:'数据操作成功',data:r,details:db});
                }
            });
            
        }

    });
});

//获取热门攒机列表
router.get('/popular',(req,res)=>{
    pool.query(' select cp.*,sp.svalue,sp.categoryName,s.imgpath from compose cp join s_c c on cp.zid=c.skuid join shopexpand sp on sp.skuid=c.skuids join shop s on s.id = sp.cid where cp.cateid in(1001,1002,1003,1004) order by cp.cateid',(err,r)=>{
		if(err){
            return res.send({code:400,msg:'出现了错误',err:err});
		}
		
		if(r.length == 0){
			return res.send({code:400,msg:'查询失败'});
		}else{
			return res.send({code:200,msg:'查询成功！',data:r});
		}
	});
});

//攒机列表详情
router.get('/popular/details',(req,res)=>{
    
    pool.query('select sp.svalue,sp.socre,s.* from compose cp join s_c c on cp.zid=c.skuid join shopexpand sp on sp.skuid=c.skuids join shop s on s.id = sp.cid where cp.zid=? order by cp.zid',[req.query.zid],(err,r)=>{
		if(err){
            return res.send({code:400,msg:'出现了错误',err:err});
		}
    
		
		if(r.length == 0){
			return res.send({code:400,msg:'查询失败'});
		}else{
            pool.query('select * from compose where zid=?',[req.query.zid],(err,q)=>{
                if(err){
                    return res.send({code:400,msg:'出现了错误',err:err});
                    
                }

                res.send({code:200,details:q,data:r,msg:'操作数据成功'});
            });
			
		}
	});
});

//热门外设
router.get('/popular/list',(req,res)=>{

    pool.query('select collocateName,MoneyCount,cateid from compose where cateid in(1005,1006,1007,1008)',(err,r)=>{
        if(err){
             return res.send({code:400,msg:'出现了错误',err:err});
             
        }
        if(r.length == 0){
            return res.send({code:400,msg:'数据操作失败！'})
        }else{
            pool.query('select c.skuid,sp.svalue,s.imgpath from  s_c c join shopexpand sp on sp.skuid=c.skuids join shop s on sp.cid = s.id where c.skuid in (15,16,17,18)  order by c.skuid',(err,db)=>{
                if(err){
                    return res.send({code:400,msg:'出现了错误',err:err});
                };
                if(db.length == 0){
                    return res.send({code:200,msg:'查询数据失败'});
                }else{
                    return res.send({code:200,msg:'数据操作成功',listHd:r,listBody:db});
                }
            });
            
        }
    });
});

router.get('/popular/dls',(req,res)=>{
    
            pool.query('select cp.collocateName,cp.MoneyCount,cp.cateid,sp.svalue,s.imgpath,s.sname from compose cp join s_c c on cp.zid=c.skuid join shopexpand sp on sp.skuid=c.skuids join shop s on s.id = sp.cid where c.skuid=? order by cp.cateid',[req.query.skuid],(err,r)=>{
                if(err){
                    
                   return res.send({code:400,msg:'出现了错误',err:err});
                };
                
                if(r.length == 0){
                    return res.send({code:400,msg:'出现了错误',err:err});
                }else{
                    pool.query()
                    return res.send({code:200,msg:'数据操作成功',data:r});
                }
            });
            
});

//暴露此路由
module.exports = router;